<?php

class Tool {

    public static function mkpath($path) {
        $dirs = array();
        $path = preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path);
        $dirs = explode("/", $path);
        $path = "";
        foreach ($dirs as $element) {
            $path.=$element . "/";
            if (!is_dir($path)) {
                if (!mkdir($path, 0777)) {
                    return false;
                } else {
                    chmod($path, 0777);
                }
            }
        }
        return true;
    }

    public static function delete_htm($scr) {
//        $str = '';
//        for ($i = 0; $i < strlen($scr); $i++) {
//            if (substr($scr, $i, 1) == "<") {
//                while (substr($scr, $i, 1) != ">")$i++;
//                $i++;
//            }
//            $str = $str . substr($scr, $i, 1);
//        }
//        $str = str_replace('<', '', $str);
//        $str = str_replace('>', '', $str);
//        return($str);
        return strip_tags($scr);
    }
    public static function get_thumb($str) {
        $image_parts = pathinfo($str);
        return $image_parts["dirname"] . '/' . $image_parts["filename"] . '_thumb.' . $image_parts["extension"];
    }
    public static function character_limiter($str, $n = 500, $end_char = '&#8230;') {
        if (strlen($str) < $n) {
            return $str;
        }

        $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));

        if (strlen($str) <= $n) {
            return $str;
        }

        $out = "";
        foreach (explode(' ', trim($str)) as $val) {
            $out .= $val . ' ';

            if (strlen($out) >= $n) {
                $out = trim($out);
                return (strlen($out) == strlen($str)) ? $out : $out . $end_char;
            }
        }
    }

    public static function string2array($tags) {
        return preg_split('/\s*,\s*/', trim($tags), -1, PREG_SPLIT_NO_EMPTY);
    }

    public static function array2string($tags) {
        return implode(', ', $tags);
    }
    function url_title($str, $separator = 'dash', $lowercase = FALSE)
	{
		if ($separator == 'dash')
		{
			$search		= '_';
			$replace	= '-';
		}
		else
		{
			$search		= '-';
			$replace	= '_';
		}

		$trans = array(
						'&\#\d+?;'				=> '',
						'&\S+?;'				=> '',
						'\s+'					=> $replace,
						'[^a-z0-9\-\._]'		=> '',
						$replace.'+'			=> $replace,
						$replace.'$'			=> $replace,
						'^'.$replace			=> $replace,
						'\.+$'					=> ''
					  );

		$str = strip_tags($str);

		foreach ($trans as $key => $val)
		{
			$str = preg_replace("#".$key."#i", $val, $str);
		}

		if ($lowercase === TRUE)
		{
			$str = strtolower($str);
		}
		
		return trim(stripslashes($str));
	}
    function ago($time, $since=null) {
        $patterns = array(
            'seconds' => 'less than a minute',
            'minute' => 'about a minute',
            'minutes' => '%d minutes',
            'hour' => 'about an hour',
            'hours' => 'about %d hours',
            'day' => 'a day',
            'days' => '%d days',
            'month' => 'about a month',
            'months' => '%d months',
            'year' => 'about a year',
            'years' => '%d years',
        );
        if ($since === null)
            $since = time();
        if (!is_int($since) && !ctype_digit($time))
            $since = strtotime($since);
        if (!is_int($time) && !ctype_digit($time))
            $time = strtotime($time);
        $seconds = abs($since - $time);
        $minutes = $seconds / 60;
        $hours = $minutes / 60;
        $days = $hours / 24;
        $weeks = $days / 7;
        $months = $days / 30;
        $years = $days / 365;

        if ($seconds < 45)
            $words = $patterns['seconds'];
        else if ($seconds < 90)
            $words = $patterns['minute'];
        else if ($minutes < 45)
            $words = sprintf($patterns['minutes'], $minutes);
        else if ($minutes < 90)
            $words = $patterns['hour'];
        else if ($hours < 24)
            $words = sprintf($patterns['hours'], $hours);
        else if ($hours < 48)
            $words = $patterns['day'];
        else if ($days < 30)
            $words = sprintf($patterns['days'], $days);
        else if ($days < 60)
            $words = $patterns['month'];
        else if ($days < 365)
            $words = sprintf($patterns['months'], $months);
        else if ($years < 2)
            $words = $patterns['year'];
        else
            $words = sprintf($patterns['years'], $years);
        $suffix = $since - $time > 0 ? 'ago' : 'from now';
        if ($since - $time > 0)
            return $words . ' ago';
        else
            return $words . ' from now';
    }

}